package com.lovo;

import java.util.Scanner;

public class ATM {
    public static Scanner scanner = new Scanner(System.in);
    //用户编号
    private int userID = -1;
    private User[] users = {
            new User("aaa","111",500),
            new User("bbb","222",100),
            new User("ccc","333",1000),
            new User("ddd","444",200),
    };

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }

    /**
     * 主流程方法
     */
    public static void process(){
        ATM atm = new ATM();
        System.out.println("欢迎进入XXX银行");
        while (true){
            login(atm);
            menu(atm);
        }

    }

    /**
     * 登录
     */
    public static void login(ATM atm){


        for (int i = 0; i < 3; i++){
            boolean sign = false;
            System.out.println("请输入账号：");
            String account = scanner.next();
            System.out.println("请输入密码：");
            String pwd = scanner.next();

            for (int j = 0; j < atm.users.length; j++){
                if (account.equals(atm.users[j].getUserName()) && pwd.equals(atm.users[j].getPassWord())){
                    sign = true;
                    atm.setUserID(j);
                    break;
                }
            }
            if (sign){
                System.out.println("登录成功");
                return;
            }else {
                System.out.println("登录失败，你还有" + (2 - i) + "次机会");

            }
            if (i == 2){
                System.out.println("已错误三次，账号冻结");
                System.exit(1);
            }
        }
    }

    /**
     * 菜单
     */
    public static void menu(ATM atm){

        while(true){
            System.out.println("请选择：1-存款 2-取款 3-转账 4-查询 5-切换账户 6-修改密码 7-退出");
            int index = scanner.nextInt();
            if (index == 1){
                save(atm);
            }else if (index == 2){
                get(atm);
            }else if (index == 3){
                transfer(atm);
            }else if (index == 4){
                search(atm);
            }else if (index == 5){
                return;
            }else if (index == 6){
                changePwd(atm);
            }else if (index == 7){
                System.out.println("再见");
                System.exit(0);
            }else {
                System.out.println("输入有误");
            }
        }

    }

    /**
     * 存款
     */
    public static void save(ATM atm){
        System.out.println("请输入您要存款的金额：");
        double blance = scanner.nextDouble();
        if (blance > 0){
            atm.users[atm.userID].setBalance(Double.parseDouble(atm.users[atm.userID].getBalance() + blance + ""));
            System.out.println("存款成功！");
        }else {
            System.out.println("金额输入有误");
        }
    }

    /**
     * 取款
     */
    public static void get(ATM atm){
        System.out.println("请输入您要取款的金额：");
        double blance = scanner.nextDouble();
        if (blance <= atm.users[atm.userID].getBalance() && blance > 0){
            atm.users[atm.userID].setBalance(atm.users[atm.userID].getBalance() - blance);
            System.out.println("取款成功！");
        }else {
            System.out.println("取款失败");
        }
    }

    /**
     * 查询
     */
    public static void search(ATM atm){
        System.out.println("您的存款为" + atm.users[atm.userID].getBalance() + "元");
    }

    /**
     * 转账
     */
    public static void transfer(ATM atm){
        boolean pd = false;
        int nextUser = -1;
        System.out.println("请输入您要转账的账户：");
        String name = scanner.next();
        if (name.equals(atm.users[atm.userID].getUserName())){
            System.out.println("不能转给自己");
            return;
        }
        for (int j = 0; j < atm.users.length; j++){
            if (name.equals(atm.users[j].getUserName())){
                nextUser = j;
                pd = true;
                break;
            }
        }
        if (pd) {
            System.out.println("请输入转账金额：");
            double blance = scanner.nextDouble();
            if (blance <= atm.users[atm.userID].getBalance() && blance > 0){
                atm.users[atm.userID].setBalance(atm.users[atm.userID].getBalance() - blance);
                atm.users[nextUser].setBalance(atm.users[nextUser].getBalance() + blance);
                System.out.println("转账成功");
            }else {
                System.out.println("转账失败，请检查输入金额或余额");
            }

        }else {
            System.out.println("未查找到相关账户");
        }
    }

    /**
     * 修改密码
     */
    public static void changePwd(ATM atm){
        boolean bj = false;
        String newPwd1 = "";
        String newPwd2 = "";
        System.out.println("请输入原密码：");
        String pwd = scanner.next();
        if (atm.users[atm.userID].getPassWord().equals(pwd)){
            bj = true;
        }else {
            System.out.println("密码错误");
            return;
        }
        if (bj){
            System.out.println("请输入新密码");
            newPwd1 = scanner.next();
            System.out.println("再次输入新密码");
            newPwd2 = scanner.next();
            if (newPwd1.equals(newPwd2)){
                System.out.println("密码修改成功");
                atm.users[atm.userID].setPassWord(newPwd1);
            }else {
                System.out.println("新密码不相同，修改失败");
                return;
            }
        }
    }





}
